<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
            <el-form-item label="公告内容" prop="content">
                <el-input v-model="queryParams.content" placeholder="请输入内容" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="公告类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="系统类型" clearable style="width: 200px">
                    <el-option v-for="dict in [{ 'label': '平台公告', 'value': 1 }, { 'label': '系统公告', 'value': 2 }]"
                        :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8" v-if="routeShow">
            <el-col :span="1.5">
                <el-button type="primary" icon="Plus" @click="handleAdd">发布</el-button>
            </el-col>
            <!-- <el-col :span="1.5">
                <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col> -->
            <!-- <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar> -->
        </el-row>

        <el-table v-loading="loading" :data="noticeList">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column label="序号" align="center" type="index" width="55">
                <template #default="{ $index }">
                    {{ (queryParams.currentPage - 1) * queryParams.pageSize + $index + 1 }}
                </template>
            </el-table-column>
            <el-table-column label="公告类型" align="center" prop="type">
                <template #default="scope">
                    <span v-if="scope.row.type == 1">平台公告</span>
                    <span v-if="scope.row.type == 2">系统公告</span>
                </template>
            </el-table-column>
            <el-table-column label="公告内容" align="center" prop="content" show-overflow-tooltip>
                <template #default="scope">
                    <span @click="showContent(scope.row)">{{ scope.row.content }}</span>
                </template>
            </el-table-column>


            <el-table-column label="创建时间" align="center" prop="createTime">
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    正常
                </template>
            </el-table-column>
            <el-table-column label="操作人员" align="center" prop="updateUserName">
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="routeShow">
                <template #default="scope">
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.currentPage"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改系统对话框 -->
        <el-dialog :title="title" v-model="open" width="780px" append-to-body>
            <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
                <el-row :gutter="12">
                    <el-col :span="12">
                        <el-form-item label="所属行业" prop="industryId">
                            <el-select v-model="form.industryId" placeholder="请输入所属行业" filterable clearable>
                                <el-option v-for="item in listIndustryArr" :key="item.industryId" :label="item.name"
                                    :value="item.industryId" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="所属系统">
                            <el-select v-model="form.industrySystemId" placeholder="请选择">
                                <el-option class="flex-1" v-for="item in systemList" :key="item.industrySystemId"
                                    :label="item.name" :value="item.industrySystemId" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="系统类型" prop="type">
                            <el-select v-model="form.type" placeholder="请选择">
                                <el-option
                                    v-for="dict in [{ 'label': '平台公告', 'value': 1 }, { 'label': '系统公告', 'value': 2 }]"
                                    :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="内容" prop="content">
                            <el-input type="textarea" v-model="form.content" placeholder="请输入内容"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Notice">

import {
    authApiindustrysystemlist,
} from "@/api/login.js";
import { ElMessageBox } from 'element-plus'

const systemList = ref([])

function authApiindustrysystemlistApi(query = '') {
    authApiindustrysystemlist(query).then(
        (res) => {
            systemList.value = [...res];
        }
    );
}

authApiindustrysystemlistApi()
import {
    authApiIndustryList
} from "@/api/login.js";

const industryList = ref([])
function authApiIndustryListApi() {
    authApiIndustryList().then(
        (res) => {
            industryList.value = [...res];
        }
    );
}

onMounted(() => {
    authApiIndustryListApi()
})

// 获取行业列表
import {
    listIndustry
} from "@/api/backIndustry/backIndustry.js";
let listIndustryArr = []
listIndustry().then((res) => {
    listIndustryArr = res.records
})

const route = useRoute();
const routeShow = ref(false)

console.log(route.meta, "route.meta");

if (route.meta.menuId == "1234") {
    // 平台
    routeShow.value = true
} else {
    // 单位
    routeShow.value = false
}


import {
    informationApimerchantsystemlist,
    addSystem,
    updateSystem,
    delNotice,

    getNotice,
} from "@/api/backInformation/backInformation";
// backInformation

const { proxy } = getCurrentInstance();
const { sys_notice_status, sys_notice_type } = proxy.useDict(
    "sys_notice_status",
    "sys_notice_type"
);

const noticeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
    form: {},
    queryParams: {
        currentPage: 1,
        pageSize: 10,
        content: undefined,
        type: undefined,
    },
    rules: {
        // noticeTitle: [
        //     { required: true, message: "系统标题不能为空", trigger: "blur" },
        // ],
        // noticeType: [
        //     { required: true, message: "系统类型不能为空", trigger: "change" },
        // ],
    },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询系统列表 */


// ​/backAdmin​/informationApi​/merchant​/system​/list
// 商家系统公告列表
function getList() {
    loading.value = true;
    informationApimerchantsystemlist(queryParams.value).then((response) => {
        noticeList.value = response.records;
        total.value = response.total;
        loading.value = false;
    });
}

/** 取消按钮 */
function cancel() {
    open.value = false;
    reset();
}

/** 表单重置 */
function reset() {
    form.value = {
        "content": "",
        "type": 1
    };
    proxy.resetForm("noticeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.currentPage = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    ids.value = selection.map((item) => item.dailyInformationId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "发布公告";
}

/**修改按钮操作 */
function handleUpdate(row) {
    reset();
    const dailyInformationId = row.dailyInformationId || ids.value;
    Object.assign(form.value, row)
    open.value = true;
    title.value = "发布公告";
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["noticeRef"].validate((valid) => {
        if (valid) {
            if (form.value.dailyInformationId != undefined) {
                updateSystem(form.value).then((response) => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addSystem(form.value).then((response) => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const dailyInformationIds = row.dailyInformationId || ids.value;
    proxy.$modal
        .confirm('是否确认删除系统编号为"' + dailyInformationIds + '"的数据项？')
        .then(function () {
            return delNotice(dailyInformationIds);
        })
        .then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
}

/** 查看内容 */
function showContent(row) {
    ElMessageBox.alert(row.content, '公告内容', {
        confirmButtonText: '',
        showConfirmButton: false,
    })
}
getList();
</script>